package com.pengtu.web.project.labs;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.SupervisoryOrganization;
import com.pengtu.service.project.SignOrganizationManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 单位管理
 * 
 * @author libitum
 * 
 */
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", location = "organization.action"),
	@Result(name = "orgsuc", location = "/orgsuccess.jsp?orgId=${id}&orgName=${orgName}", type = "redirect")})
@Result(name = BaseActionSupport.VIEW, location = "${ctx}/project/labs/organization!signlist.action?typeId=${typeId}", type = "redirect")
public class SupervisoryorgAction extends BaseActionSupport<SupervisoryOrganization> {

	private static final long serialVersionUID = 1L;

	private SignOrganizationManager signOrgManager;

	// -- 页面属性 --//
	private String id;
	private SupervisoryOrganization supervisoryOrg;
	private Page<SupervisoryOrganization> page = new Page<SupervisoryOrganization>(10);// 每页5条记录
	private String typeId;

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	@Autowired
	public void setSignOrgManager(SignOrganizationManager signOrgManager) {
		this.signOrgManager = signOrgManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<SupervisoryOrganization> getPage() {
		return page;
	}

	public void setPage(Page<SupervisoryOrganization> page) {
		this.page = page;
	}

	@Override
	public SupervisoryOrganization getModel() {
		return supervisoryOrg;
	}

	public String list() throws Exception {
		QueryFilter queryfilters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = signOrgManager.findPageSupervisoryOrg(page, queryfilters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		signOrgManager.saveSupervisorOrg(supervisoryOrg);
		addActionMessage("保存单位成功");
		if(StringUtils.isNotEmpty(typeId)){
			id = supervisoryOrg.getId();
			return "orgsuc";
		};
		return input();
	}

	/**
	 * 验证机构代码是否重复
	 */
	public void checkOrgNum() {
		// TODO Auto-generated method stub
		String orgNum = Struts2Utils.getParameter("orgNum");
		String oldOrgNum = Struts2Utils.getParameter("oldOrgNum");
		if (signOrgManager.isOrgNumUnique(orgNum, oldOrgNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	/**
	 * 验证单位名是否重复
	 */
	public void checkOrgName() {
		// TODO Auto-generated method stub
		String orgName = Struts2Utils.getParameter("orgName");
		String oldOrgName = Struts2Utils.getParameter("oldOrgName");
		if (signOrgManager.isOrgNameUnique(orgName, oldOrgName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("删除设计单位" + supervisoryOrg.getOrgName());
		signOrgManager.deleteSupervisorOrg(id);
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			supervisoryOrg = signOrgManager.getSupervisory("id",id);
		} else {
			supervisoryOrg = new SupervisoryOrganization();
		}
	}

}
